import urllib2
import urllib
import sys
import random
import httplib

try: import json #python 2.6 included simplejson as json
except ImportError: import simplejson as json

def main():
    fo = sys.stdin
   
    if len(sys.argv)==2:
        fo = open(sys.argv[1])
    genelist = []
    for line in fo:
        genelist.append(line.rstrip())

        
    doPost(genelist)
    fo.close()

def doPost(genelist):   
    data = json.dumps({"method":"functionate",
            "params":[{ "query": genelist, "species": "Homo sapiens" , "namespace": "hgnc_symbol" }],
            "id":"%032x"%(random.getrandbits(128)),
            "jsonrpc": "2.0"})
    
    headers = {"Content-type": "application/json","Accept": "application/json"}
    conn = httplib.HTTPConnection("llama.mshri.on.ca")
    conn.request("POST", "/cgi/funcassociate/serv", data, headers)
    returndata = json.load(conn.getresponse())

    print json.dumps(returndata, sort_keys=True, indent=4)

if __name__ == "__main__":
    
    main()
